﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace WOS.Plugins.Remote
{
    public class RemoteManagerClient
    {
        public string ServerIP
        {
            get;
            set;
        }
        public int ServerPort
        {
            get;
            set;
        }

        #region "Send"
        Socket socket;
        byte[] bytes;
        
        SocketAsyncEventArgs sendEventArgs;
        public void Start()
        {
            this.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            SocketAsyncEventArgs args = new SocketAsyncEventArgs()
            {
                RemoteEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), Convert.ToInt32(ServerPort))
            };
            args.Completed += OnConnected;
            this.socket.ConnectAsync(args);
        }
        public void SendCommand(Command c)
        {
            SendCommand(c.Name);
        }
        public void SendCommand(string commandData)
        {
            List<ArraySegment<byte>> l = new List<ArraySegment<byte>>();
            l.Add(new ArraySegment<byte>(Encoding.UTF8.GetBytes(commandData)));
            sendEventArgs.BufferList = l;
            socket.SendAsync(sendEventArgs);
        }
        void OnConnected(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                sendEventArgs = new SocketAsyncEventArgs();
                sendEventArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Parse(ServerIP), Convert.ToInt32(ServerPort));
                e.Completed -= OnConnected;
                e.Completed += new EventHandler<SocketAsyncEventArgs>(receiveEventArgs_Completed);
                e.SetBuffer(bytes, 0, bytes.Length);
                socket.ReceiveAsync(e);
            }
        }

        void receiveEventArgs_Completed(object sender, SocketAsyncEventArgs e)
        {
            String a = Encoding.UTF8.GetString(bytes, e.Offset, e.BytesTransferred);
            OnCommandReceived(a);
            socket.ReceiveAsync(e);
        }
        void OnSendCompleted(object sender, SocketAsyncEventArgs e)
        {
            socket.Close();
            OnCommandSended(null);
        }
        public event EventHandler<CommandEventArgs> CommandSended;
        private void OnCommandSended(CommandEventArgs e)
        {
            if (CommandSended != null)
            {
                CommandSended(this, e);
            }
        }
        #endregion

        #region "Receive"
       
        public event EventHandler<CommandEventArgs> CommandReceived;
        private void OnCommandReceived(string commandData)
        {
            CommandEventArgs e = new CommandEventArgs();
            e.Command = new Command();
            e.Command.Name = commandData;
            OnCommandReceived(e);
        }
        private void OnCommandReceived(CommandEventArgs e)
        {
            if (CommandReceived != null)
            {
                CommandReceived(this, e);
            }
        }
        #endregion

    }
    public class RemoteClient
    {
        public string IP { get; set; }
        public int Port { get; set; }
    }
    public class CommandEventArgs : EventArgs
    {
        public RemoteClient Client { get; set; }
        public Command Command { get; set; }
    }

}
